package cn.zhfeat.hatch.web.utils.merchant;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.apache.commons.lang.StringUtils;

public class ConfigUtil {

	private static ConfigUtil instance = null;
	private Properties p = null;
	public String domain = "http://dev.cecook.cn";
	public int pageSize = 20;
	public String tmpFilePath = "/data/scrm/wx_file/";
	public int cookieExpire = 31536000;//缺省是一年 1 * 365 * 24 * 60 * 60
	public String user_info_default_logo = "http://oss.300.cn/cook_test:cecook-test/3db69f4b-19f6-4609-bccd-678036700e5e";
	public String user_info_default_background = "http://oss.300.cn/cook_test:cecook-test/93d2503e-b6d5-4079-96ec-a10b8c6a6f2f";

	public static ConfigUtil getInstance(){
		if(instance == null){
			synchronized (ConfigUtil.class) {
				if(instance == null){
					instance = new ConfigUtil();
				}
			}
		}
		return instance;
	}

	private ConfigUtil(){
		p = loadProperties("conf/weixinOpen.properties");
		domain = p.getProperty("DOMAIN", "http://dev.cecook.cn");
		tmpFilePath = p.getProperty("TMP_FILE_PATH", "/data/scrm/wx_file/");
		if(!new File(tmpFilePath).exists()){
			new File(tmpFilePath).mkdirs();
		}
		try {
			pageSize = StringUtils.isEmpty(p.getProperty("PAGE_SIZE"))?20:Integer.valueOf(p.getProperty("PAGE_SIZE"));
		} catch (Exception e) {
			pageSize = 20;
		}
		try {
			cookieExpire = StringUtils.isEmpty(p.getProperty("WX_COOKIE_EXPIRE"))?31536000:Integer.valueOf(p.getProperty("WX_COOKIE_EXPIRE"));
		} catch (Exception e) {
			cookieExpire = 31536000;
		}
	}

	public Properties getProperties(){
		return p;
	}

	private Properties loadProperties(String PropertiesUrl){
		InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(PropertiesUrl);
		if (p == null) {
			p = new Properties();
			if (inputStream != null) {
				try {
					p.load(inputStream);

				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					System.out.println("InputStream加载异常");
				} finally {
					try {
						inputStream.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
					try {
						inputStream = null;
					} catch (Exception e2) {
						e2.printStackTrace();
					}
				}
			}
		}
		return p;
	}
}
